/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.ecommerce.entity.impl;

import br.com.ecommerce.entity.IEntity;
import br.com.ecommerce.entity.enums.StatusTypeVenda;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 *
 * @author rpateixeira
 */
@Entity
@Table(name="VENDAS")
public class Venda implements IEntity,Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "VENDA_GENERATOR", sequenceName = "public.vendaSequence", initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator="VENDA_GENERATOR")
    @Column(name="SEQ_VENDA")
    private Long id;
    @NotNull
    @ManyToOne(optional=false)
    @JoinColumn(name="ID_USUARIO",referencedColumnName="SEQ_USUARIO")
 //   @Valid
    private Usuario usuario;
    @Temporal(TemporalType.DATE)
    @NotNull
    @Column(nullable=false)
    private Date dataVenda;
    @Enumerated(EnumType.STRING)
    @Column(name="STATUS_VENDA",nullable=false)
    @NotNull
    private StatusTypeVenda status= StatusTypeVenda.START;
    @NotNull
    @Min(0)
    @Column(nullable=false)
    private BigDecimal total;
    @ManyToOne(optional=false)
    @NotNull
    @JoinColumn(name="ID_ENDERECO",referencedColumnName="SEQ_ENDERECO")
 //   @Valid
    private Endereco enderecoToSend;
    @ManyToOne(optional=false)
    @JoinColumn(name="ID_CreditCard",referencedColumnName="SEQ_CARD")
//    @Valid
    @NotNull
    private CreditCard creditCard;
    @OneToMany(cascade=CascadeType.ALL,mappedBy="venda")
    private List<ItemVenda> itensVenda;

    @Column(name="VENDA_FECHADA")
    private boolean vendaFechada=false;

    @Override
    public Long getId() {
        return this.id;
    }

    public CreditCard getCreditCard() {
        return creditCard;
    }

    public void setCreditCard(CreditCard creditCard) {
        this.creditCard = creditCard;
    }
    
    public Date getDataVenda() {
        return dataVenda;
    }

    public void setDataVenda(Date dataVenda) {
        this.dataVenda = dataVenda;
    }
    
    public Endereco getEnderecoToSend() {
        return enderecoToSend;
    }

    public void setEnderecoToSend(Endereco enderecoToSend) {
        this.enderecoToSend = enderecoToSend;
    }
    
    public StatusTypeVenda getStatus() {
        return status;
    }

    public void setStatus(StatusTypeVenda status) {
        this.status = status;
    }
    
    public BigDecimal getTotal() {
        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }
    
    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }
    
    public List<ItemVenda> getItensVenda() {
        if(itensVenda==null){
            itensVenda=new LinkedList<ItemVenda>();
        }
        return itensVenda;
    }

    public void setItensVenda(List<ItemVenda> itensVenda) {
        this.itensVenda = itensVenda;
    }

    public boolean isVendaFechada() {
        return vendaFechada;
    }

    public void setVendaFechada(boolean vendaFechada) {
        this.vendaFechada = vendaFechada;
    }

    public void addItem(ItemVenda itemVenda){
        itemVenda.setVenda(this);
        getItensVenda().add(itemVenda);
        if(total ==null){
            total = new BigDecimal("0");
            total=total.add(itemVenda.getProduto().getCusto());
        }else{
            total=total.add(itemVenda.getProduto().getCusto());
        }
    }

}
